-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Box<[Type]> inside of
FunctionType`
#2036
Conversation
This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and additional space on the heap by not allowing resizing (like Vec does)
bors r+ |
2036: Use Box<[Type]>` inside of `FunctionType` r=syrusakbary a=MarkMcCaskey This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and additional space on the heap by not allowing resizing (like Vec does) There are ways to save even more memory here but they're all heavier weight than this simple change. Co-authored-by: Mark McCaskey <[email protected]>
params: params.into(), | ||
results: returns.into(), | ||
params: params.into().into_boxed_slice(), | ||
results: returns.into().into_boxed_slice(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust can infer the target type of returns.into()
to the temporary Vec<Type>
? Impressive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, params
and returns
and generic and the only thing we know about these types is that they implement Into<Vec<Type>>
, but I agree that it is nice!
@@ -229,9 +229,9 @@ impl ExternType { | |||
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] | |||
pub struct FunctionType { | |||
/// The parameters of the function | |||
params: Vec<Type>, | |||
params: Box<[Type]>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this affect serde somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe it should, but that's a good thing to check -- thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, checked the serde source code: they're both serialized as sequences, so there shouldn't be any effect on the serialization in any format
Build failed: |
Can wait until we can use Github Actions in our own M1 machines |
bors r+ |
2036: Use Box<[Type]>` inside of `FunctionType` r=MarkMcCaskey a=MarkMcCaskey This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and additional space on the heap by not allowing resizing (like Vec does) There are ways to save even more memory here but they're all heavier weight than this simple change. Co-authored-by: Mark McCaskey <[email protected]>
Build failed: |
bors r+ |
2036: Use Box<[Type]>` inside of `FunctionType` r=MarkMcCaskey a=MarkMcCaskey This saves 16 bytes (on 64bit systems) on the stack for each `FunctionType` and additional space on the heap by not allowing resizing (like Vec does) There are ways to save even more memory here but they're all heavier weight than this simple change. Co-authored-by: Mark McCaskey <[email protected]>
Build failed: |
Merging manually since all tests pass except of the macos-11 due to issues in Github Actions (not in our codebase) |
2141: feat(types) Avoid allocating a `Vec` when calling `FunctionType::new` r=Hywan a=Hywan # Description Sequel of #2036. We can go further by defining `Params` and `Results` of `FunctionType::new` as `Into<Box<[Type]>>` directly rather than `Into<Vec<Type>>`. It simplifies the code: `params.into()` rather than `params.into().into_boxed_slice()`. I assume it doesn't allocate an intermediate vector. Since `From<Box<[T]>>` is implemented for `Vec<T>`, I reckon it's not a BC break. # Review - [ ] ~Add a short description of the the change to the CHANGELOG.md file~ not necessary Co-authored-by: Ivan Enderlin <[email protected]>
2141: feat(types) Avoid allocating a `Vec` when calling `FunctionType::new` r=syrusakbary a=Hywan # Description Sequel of #2036. We can go further by defining `Params` and `Results` of `FunctionType::new` as `Into<Box<[Type]>>` directly rather than `Into<Vec<Type>>`. It simplifies the code: `params.into()` rather than `params.into().into_boxed_slice()`. I assume it doesn't allocate an intermediate vector. Since `From<Box<[T]>>` is implemented for `Vec<T>`, I reckon it's not a BC break. # Review - [ ] ~Add a short description of the the change to the CHANGELOG.md file~ not necessary Co-authored-by: Ivan Enderlin <[email protected]>
This saves 16 bytes (on 64bit systems) on the stack for each
FunctionType
andadditional space on the heap by not allowing resizing (like Vec does)
There are ways to save even more memory here but they're all heavier weight than this simple change.